package cn.xiaobai.service;

import cn.xiaobai.domain.User;
import cn.xiaobai.mapper.UserMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.service.IService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author yangdaji
 * @Date 2024/2/24
 * @PackageName cn.xiaobai.service
 * @ClassName UserServer
 */
@Service
public class UserServer extends ServiceImpl<UserMapper, User> implements IService<User> {

    @Autowired
    private UserMapper userMapper;


    /**
     * 用户登录
     *
     * @param user 用户信息
     * @return
     * @throws NotFoundException 用户不存在时抛出
     */
    public User login(User user) throws NotFoundException {
        // 查询用户是否存在
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .eq("username", user.getUsername())
                .eq("password", user.getPassword());
        User byQuery = userMapper.selectOneByQuery(queryWrapper);

        if (byQuery == null) {
            throw new NotFoundException("用户不存在!");
        }
        return byQuery;

    }
}
